<?php
include 'common.php';

	//获取数据
	$user_name = $_SESSION['home']['name'];
	$time = time();

	$rec_id = $_POST['address'];

	//更新order表
	$sql = "insert into ".PRE."order(user_name,rec_id,addtime,status) values('{$user_name}','{$rec_id}','{$time}','0')";
	mysql_query($sql);
	//生成opder_id
	$order_id = mysql_insert_id($link);


	//更新order_goods表
	foreach($_SESSION['cart'] as $key => $val):
	$sql = "insert into ".PRE."order_goods(order_id,goods_id,price,num) values('{$order_id}','{$key}','{$val['price']}','{$val['num']}')";

	mysql_query($sql);
    
    //更新库存
    $sql = "update ".PRE."goods set store= store-'{$val['num']}' where id='{$key}'";
    mysql_query($sql);
	endforeach;

	//清空购物车
	unset($_SESSION['cart']);

	//跳转页面
	header('Location:order_success.php');

